<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入要BB的内容(最多吐槽120字)" maxlength="120" v-model="msg"></textarea>

        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>

        <!-- 评论列表 -->
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in commentList" :key="item.id">
                <div class="cmt-title">第{{ i+1 }}楼&nbsp;&nbsp;用户: {{ item.userName }}&nbsp;&nbsp;发表时间: {{ item.addTime | dateFormat }}</div>
                <div class="cmt-body">
                    {{ item.content }}
                </div>
            </div>

        </div>

        <mt-button type="danger" size="large" plain @click="more">加载更多</mt-button>
    </div>
</template>

<script>
//导入mui中Toast组件
import { Toast } from 'mint-ui';
export default {
    data() {
        return {
            commentList: [],
            pageIndex: 1,   //默认展示第一页数据
            msg: ''   //评论的内容
        
        }
    },
    methods: {
        getCommentList() {
            this.$http.get('vue/getCommentList/'+this.id+'?pageIndex=' + this.pageIndex).then(result => {
                if (result.body.status === 0) {
                    let list = result.body.message
                    //数组拼接 每次获取新数据时，均为老数据拼接上新数据
                    this.commentList = this.commentList.concat(list);
                    if (this.pageIndex != 1 && list.length === 0) {
                        Toast('刷到底了！')
                    }
                } else {
                    Toast('获取评论列表失败')
                }
            })
        },
        more() {
            this.pageIndex ++;
            this.getCommentList();
        },
        postComment() {
            if (this.msg.trim().length === 0) {
                Toast('您输入的内容为空！')
                return false;
            }

            this.$http.post('vue/addComment',{id: this.id,content: this.msg.trim()}).then(result => {
                if (result.body.status === 0) { 
                    let cmt = {
                        content: this.msg.trim(),
                        userName: 'admin',
                        addTime: Date.now()
                    };
                    this.commentList.unshift(cmt);
                    this.msg = '';
                    Toast(result.body.message);
                } else {
                    Toast('发表评论失败')
                }
            })
        }
    },
    props: ['id'],
    created() {
        this.getCommentList();
    }
}
</script>

<style lang="scss" scoped>
    .cmt-container {
        h3 {
            font-size: 18px;
        }
        textarea {
            font-size: 14px;
            height: 85px;
            margin: 0px;
        }

        .cmt-list {
            margin: 5px 0;
            .cmt-item {
                font-size: 13px;
                .cmt-title {
                    line-height: 30px;
                    background-color: #ccc;
                }

                .cmt-body {
                    line-height: 35px;
                    text-indent: 2em;
                }
            }
        }
    }
</style>